package cn.itshare.junit.testMail;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeUtility;

import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

public class MailUtil {

	public static void main(String[] args) {
		// sendsimple() ;
		searchMail();
	}

	public static void searchMail() {
		try {
			Properties props = System.getProperties();
			props.put("mail.pop3.host", "pop.126.com");
			props.put("mail.pop3.port", 110);
			props.put("mail.pop3.starttls.enable", "true");
			props.put("mail.pop3.socketFactory.port", 110);
			// props.put("mail.pop3.socketFactory.class",
			// "javax.net.ssl.SSLSocketFactory");
			props.put("mail.pop3.socketFactory.fallback", "false");
			Session session1 = Session.getInstance(props, null);
			//session1.setDebug(true);
			// 得到Store对象
			Store store = session1.getStore("pop3");// 接收邮件用pop3
			// 建立到服务器的连接
			store.connect("pop.126.com", "yhqsaber", "yhqsaber123");

			// 得到收件文件夹
			Folder folder = store.getFolder("INBOX");
			// 设置文件夹属性
			folder.open(Folder.READ_WRITE);
			// 获取文件夹中的邮件
			Message[] messages = folder.getMessages();
			
			System.out.println("收件箱的邮件数：" + messages.length);
			for (int i = 0; i < messages.length; i++) {
				// 邮件发送者
				String from = decodeText(messages[i].getFrom()[0].toString());
				InternetAddress ia = new InternetAddress(from);
				System.out.println("来自:" + ia.getPersonal() + '(' + ia.getAddress() + ')');
				// 邮件标题
				System.out.println("标题:" + messages[i].getSubject());
				// 邮件大小
				System.out.println("大小:" + messages[i].getSize());
				// 邮件发送时间
				System.out.println("日期:" + messages[i].getSentDate());
				System.out.println("********************************\n");
				outPutFileEmail(ia.getPersonal() + '(' + ia.getAddress() + ')',messages[i].getSubject(),messages[i].getSize(),messages[i].getSentDate());
			}

			System.out.println("邮件读取成功！");
			folder.close(true);
			store.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	protected static String decodeText(String text) throws UnsupportedEncodingException {
		if (text == null)
			return null;
		if (text.startsWith("=?GB") || text.startsWith("=?gb"))
			text = MimeUtility.decodeText(text);
		else
			text = new String(text.getBytes("ISO-8859-1"));
		return text;
	}

	public static void outPutFileEmail(String from,String title,int size,Date date) {
		String line = "******************************************************" ;
		try {
			File f = new File("c:\\email.txt") ;
			FileWriter fos = new FileWriter(f,true) ;
			BufferedWriter bw = new BufferedWriter(fos) ;
			
			bw.write("来自："+from + "\r\n" + "标题："+title + "\r\n" + "大小："+ size + "\r\n" + "日期："+ date + "\r\n"+line+"\r\n") ;
			
			
			bw.flush();
			fos.flush() ;
			bw.close() ;
			fos.close() ;
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	public static void sendsimple() {
		try {
			Email email = new SimpleEmail();
			email.setAuthentication("yhqsaber", "yhqsaber123");
			email.setHostName("smtp.126.com");
			email.setFrom("yhqsaber@126.com");
			email.setSubject("1111111");
			email.setMsg("hello commons-mail");
			email.addTo("yhqsaber@126.com");
			email.send();

		} catch (EmailException e) {
			e.printStackTrace();
		}
	}

}
